<template>
  <div>
    <a-drawer
      v-model:visible="visible"
      class="custom-class"
      :title="title"
      placement="right"
      @close="modalClose.close"
      @after-visible-change="afterVisibleChange"
    >
      <div>
        <a-form :label-col="{ span: 14 }" :wrapper-col="{ span: 24 }" label-align="left" :layout="'vertical'">
          <a-form-item label="标识" v-bind="validateInfos.key">
            <a-input v-model:value="modelRef.key" :disabled="isDetl" placeholder="请输入英文标识" />
          </a-form-item>
          <a-form-item label="名称" v-bind="validateInfos.name">
            <a-input v-model:value="modelRef.name" :disabled="isDetl" placeholder="请输入名称" />
          </a-form-item>
          <a-form-item label="描述" v-bind="validateInfos.describe">
            <a-textarea
              v-model:value="modelRef.describe"
              placeholder="请输入描述"
              :disabled="isDetl"
              :auto-size="{ minRows: 2, maxRows: 2 }"
            />
          </a-form-item>
        </a-form>
      </div>
      <template #footer>
        <div style="display: flex; justify-content: left">
          <div v-show="!isDetl">
            <a-button type="primary" @click="onSubmit">{{ isDetl ? '关闭' : '确定' }}</a-button>
          </div>
          <div style="margin-left: 10px">
            <a-button @click="cancal">{{ isDetl ? '关闭' : '取消' }}</a-button>
          </div>
        </div>
      </template>
    </a-drawer>
    <a-modal v-model:visible="modalClose.modalVisible" title="关闭" @ok="modalClose.modalHandleOk">
      <span>数据未保存,确认关闭吗?</span>
    </a-modal>
  </div>
</template>
<script>
import { defineComponent, toRaw, ref, watch, defineEmits, toRefs, onMounted, reactive, nextTick } from 'vue'
// import { QuestionCircleOutlined } from '@ant-design/icons-vue'
import { Form } from 'ant-design-vue'
import exp from '@/utils/regular'

const useForm = Form.useForm
import factory from '../../factory'

export default defineComponent({
  // components: { QuestionCircleOutlined },
  props: {
    isShowEdit: { type: Boolean, default: false },
    isEdit: { type: String, default: '' },
    currentItemId: { type: String, default: '' },
    typeList: { type: Array, default: () => [] },
    appTypeListTra: { type: Array, default: () => [] },
    upperRol: {
      type: Object,
      default: () => {
        return {}
      },
    },
  },
  emits: ['add-close', 'create-success'],
  setup(props, ctx) {
    const { isShowEdit, isEdit, currentItemId, upperRol } = toRefs(props)
    const modelRef = reactive({
      key: '',
      name: '',
      describe: '',
    })
    const rulesRef = reactive({
      name: [
        {
          required: true,
          message: '请输入名称',
        },
        {
          pattern: exp.name.pattern,
          message: exp.name.message,
        },
        {
          max: 25,
          message: '最多输入25个字符',
        },
      ],
      key: [
        {
          required: true,
          message: '请输入英文标识',
        },
        {
          pattern: exp.key.pattern,
          message: exp.key.message,
        },
        {
          max: 20,
          message: '最多输入20个字符',
        },
      ],
      describe: [
        {
          max: 127,
          message: '最多输入127个字符',
        },
      ],
    })
    const { validate, validateInfos } = useForm(modelRef, rulesRef, {
      //表单验证的输出: 字段 值 提示
      onValidate: (...args) => {
        console.log('表单验证的输出: 字段 值 提示', ...args)
      },
    })

    //==保存==============================
    const onSubmit = () => {
      validate()
        .then(() => {
          //表单验证无误后执行
          save()
        })
        .catch((err) => {
          console.log('error', err)
        })
    }
    const save = () => {
      factory.addPermisGroup(modelRef).then((res) => {
        visible.value = false
        ctx.emit('create-success', modelRef)
      })
    }

    //== 监视组件显示 =================================
    const visible = ref(false)
    const cancal = () => {
      modalClose.close()
    }
    const title = ref('新增')
    const isDetl = ref(false)
    const afterVisibleChange = (newValue) => {
      if (!newValue) {
        ctx.emit('add-close', false)
      }
    }

    //==所属应用选择===========================
    const appTypeValeChange = (obj) => {
      modelRef.scope = obj.value
      modelRef.scopeName = obj.label
    }

    const options = [
      {
        value: 'zhejiang',
        label: 'Zhejiang',
      },
      {
        value: 'jiangsu',
        label: 'Jiangsu',
      },
    ]

    //==确认关闭对话框======================
    const modalClose = reactive({
      backupData: '',
      close: (e) => {
        if (JSON.stringify(modelRef) !== modalClose.backupData) {
          visible.value = true
          modalClose.modalVisible = true
          return
        }
        visible.value = false
      },
      modalVisible: false,
      modalHandleOk: () => {
        visible.value = false
      },
    })
    //备份数据
    modalClose.backupData = JSON.stringify(modelRef)
    return {
      visible,
      onSubmit,
      title,
      cancal,
      validateInfos,
      modelRef,
      isDetl,
      appTypeValeChange,
      options,
      afterVisibleChange,
      modalClose,
    }
  },
})
</script>
<style scoped>
#lulantu {
  width: 300px;
  height: 200px;
  overflow: hidden;
  border: 1px solid #d9d9d9;
}
</style>
